//
//  WBBaseViewController.swift
//  Weibo-Swift
//
//  Created by law on 2018/6/1.
//  Copyright © 2018年 JKing. All rights reserved.
//

import UIKit

class WBBaseViewController: UIViewController {
    
    /// 用户登录标记
    var userLogon = true
    /// 访客视图信息
    var visitorInfo: [String: String]?
    
    var tableView: UITableView?
    var refreshControl: UIRefreshControl?
    var isPullup = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        refresh()
    }
    
    /// 加载数据 - 具体实现由子类负责
    @objc func refresh() {
        refreshControl?.endRefreshing()
    }
}

extension WBBaseViewController {
    
    @objc private func register() {
        print("用户注册")
    }
    
    @objc private func login() {
        print("用户登录")
    }
    
}

// MARK: - UI
extension WBBaseViewController {
    
    private func setupUI() {
        view.backgroundColor = UIColor.white
        
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.darkGray];
        navigationController?.navigationBar.tintColor = UIColor.orange
        
        userLogon ? setupTableView() : setupVisitorView()
    }
    
    /// 设置表格视图
    @objc func setupTableView() {
        
        tableView = UITableView(frame: view.bounds, style: .plain)
        view.addSubview(tableView!)
        
        tableView?.dataSource = self
        tableView?.delegate = self
        
        refreshControl = UIRefreshControl()
        refreshControl?.addTarget(self, action: #selector(refresh), for: .valueChanged)
        tableView?.addSubview(refreshControl!)
    }
    
    
    /// 设置访客视图
    private func setupVisitorView() {
        
        let visitorView = WBVisitorView(frame: view.bounds)
        visitorView.visitorInfo = visitorInfo
        visitorView.registerButton.addTarget(self, action: #selector(register), for: .touchUpInside)
        visitorView.loginButton.addTarget(self, action: #selector(login), for: .touchUpInside)
        
        view.addSubview(visitorView)
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: #selector(register))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", target: self, action: #selector(login))
    }
    
}

// MARK: - UITableViewDataSource, UITableViewDelegate
extension WBBaseViewController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    // 在显示最后一行的时候 上拉刷新
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let row = indexPath.row
        let section = tableView.numberOfSections - 1
        
        if row < 0 || section < 0 {
            return
        }
        
        let count = tableView.numberOfRows(inSection: section)
        if row == (count - 1) && !isPullup {
            isPullup = true
            refresh()
        }
    }
}
